package com.lms.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description: 登录拦截器-安全配置
 * @author: L-B-S
 * @Date: 2021/3/4 15:45
 * @modified_By:
 * @Version: 0.0$
 */
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 在所有请求之前执行
     *
     * @param request  请求对象
     * @param response 响应对象
     * @param handler  操作兑现
     * @return 标志码
     * @throws Exception 异常对象
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.登录检查逻辑
        HttpSession session = request.getSession();
        // 2.将登录信息封装起来
        Object loginInfo = session.getAttribute("loginInfo");
        // 3.判空
        if (loginInfo != null) {
            // 如果有数据证明已登录，放行请求
            return true;
        }
        // 4.没有数据则证明没有登录，将所有请求拦截
        request.getRequestDispatcher("/admin/login").forward(request, response);
        return false;
    }
}